﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics;
using LowLevelGraphics.Filter;

using LowLevelGraphics.Drawing.FloodFillModes;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Flood fill filter delegate
    /// </summary>
    public delegate bool FloodFillFilterDelegate(UnsafeBitmap _bitmap, int _nX, int _nY, Color _color);

    /// <summary>
    /// Concrete flood fill filter
    /// </summary>
    public class FloodFillFilter : BaseImageFilter
    {
        protected IEnumerable<Point> m_aPoint = new Point[] { Point.Empty };

        protected IFill m_IFill = new FloodFillMode();
        protected Color m_Color = Color.Red;

        protected FloodFillFilterDelegate m_FloodFillFilter = null;
        
        /// <summary>
        /// Initializes a new instance of the <see cref="FloodFillFilter"/> class.
        /// </summary>
        public FloodFillFilter()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FloodFillFilter"/> class.
        /// </summary>
        /// <param name="_floodFillFilter">The _flood fill filter.</param>
        public FloodFillFilter(FloodFillFilterDelegate _floodFillFilter)
            : this(_floodFillFilter, 1, 1)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FloodFillFilter"/> class.
        /// </summary>
        /// <param name="_floodFillFilter">The _flood fill filter.</param>
        /// <param name="_nXStep">The _n X step.</param>
        /// <param name="_nYStep">The _n Y step.</param>
        public FloodFillFilter(FloodFillFilterDelegate _floodFillFilter, int _nXStep, int _nYStep)
        {
            m_FloodFillFilter = _floodFillFilter;
            m_szStep.Width = _nXStep;
            m_szStep.Height = _nYStep;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FloodFillFilter"/> class.
        /// </summary>
        /// <param name="_floodFill">The _flood fill.</param>
        /// <param name="_aPoint">The _a point.</param>
        /// <param name="_color">The _color.</param>
        public FloodFillFilter(IFill _floodFill, IEnumerable<Point> _aPoint, Color _color)
        {        
            m_IFill = _floodFill;
            m_aPoint = _aPoint;
            m_Color = _color;
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;
            int y = 0;
            int x = 0;
            int nXStep = m_szStep.Width;
            int nYStep = m_szStep.Height;
            if (m_FloodFillFilter != null)
            {
                for (y = 0; y < nHeight; y += nYStep)
                {
                    for (x = 0; x < nWidth; x += nXStep)
                    {
                        if (m_FloodFillFilter(_bitmap, x, y, m_Color))
                        {
                            m_IFill.Fill(_bitmap, x, y, m_Color);
                        }
                    }
                }
            }
            else
            {
                foreach (Point point in m_aPoint)
                {
                    m_IFill.Fill(_bitmap, point.X, point.Y, m_Color);
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new FloodFillFilter(m_IFill, m_aPoint, m_Color);
        }
    }
}
